/*Copyright 2014 Douglas Soares

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package br.ds.twitter4a.rest.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Class that provides utilities for shared preferences on behalf t4Android.
 */
public final class TwitterPreferences {

    /**
     * The single instance of {@link TwitterPreferences}.
     */
    private static final TwitterPreferences instance;

    /**
     * The preferences.
     */
    private static SharedPreferences preferences;

    // Init

    static {
	instance = new TwitterPreferences();
    }

    /**
     * Private constructor, avoid external instantiation.
     */
    private TwitterPreferences() {
    }

    public static final TwitterPreferences getInstance(final Context context) {
	if (context == null) {
	    throw new IllegalArgumentException("Context must not be null!");
	}
	preferences = context.getSharedPreferences(
		TwitterPreferences.class.getName(), Context.MODE_PRIVATE);
	return instance;
    }

    // / public utility methods

    /**
     * Persists the request token.
     * 
     * @param requestToken
     *            the request token to persist.
     * @return <code>true</code> if the data was successfully set and
     *         <code>false</code> otherwise.
     */
    public boolean updateRequestToken(final String requestToken) {
	return preferences.edit().putString(REQUEST_TOKEN, requestToken)
		.commit();
    }

    /**
     * Persists the token secret.
     * 
     * @param tokenSecret
     *            the token secret to persist.
     * @return <code>true</code> if the data was successfully set and
     *         <code>false</code> otherwise.
     */
    public boolean updateTokenSecret(final String tokenSecret) {
	return preferences.edit().putString(TOKEN_SECRET, tokenSecret).commit();
    }

    /**
     * Persists the access token.
     * 
     * @param accessToken
     *            the access token to persist.
     * @return <code>true</code> if the data was successfully set and
     *         <code>false</code> otherwise.
     */
    public boolean updateAccessToken(final String accessToken) {
	return preferences.edit().putString(ACCESS_TOKEN, accessToken).commit();
    }

    /**
     * Persists the oauth token.
     * 
     * @param oauthToken
     *            the oauth token to persist.
     * @return <code>true</code> if the data was successfully set and
     *         <code>false</code> otherwise.
     */
    public boolean updateOauthToken(final String oauthToken) {
	return preferences.edit().putString(OAUTH_TOKEN, oauthToken).commit();
    }

    /**
     * @return the requestToken, <code>null</code> if it is not set.
     */
    public String retrieveRequestToken() {
	return preferences.getString(REQUEST_TOKEN, null);
    }

    /**
     * @return the tokenSecret, <code>null</code> if it is not set.
     */
    public String retrieveTokenSecret() {
	return preferences.getString(TOKEN_SECRET, null);
    }

    /**
     * @return the tokenSecret, <code>null</code> if it is not set.
     */
    public String retrieveAcssesToken() {
	return preferences.getString(ACCESS_TOKEN, null);
    }

    /**
     * @return the oauthToken, <code>null</code> if it is not set.
     */
    public String retrieveOauthToken() {
	return preferences.getString(OAUTH_TOKEN, null);
    }

    // /Constants

    /**
     * The OAUTH_TOKEN
     */
    private static final String OAUTH_TOKEN = "oauthToken";
    /**
     * The TOKEN_SECRET
     */
    private static final String TOKEN_SECRET = "tokenSecret";
    /**
     * The ACCESS_TOKEN
     */
    private static final String ACCESS_TOKEN = "accessToken";
    /**
     * The REQUEST_TOKEN
     */
    private static final String REQUEST_TOKEN = "requestToken";
}
